package com.useeinfo.asg.open.web;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.FrontUserBiz;
import com.useeinfo.asg.modules.logging.entity.FrontUser;
import com.useeinfo.asg.modules.security.biz.SystemRoleBiz;
import com.useeinfo.asg.modules.security.biz.SystemUserBiz;
import com.useeinfo.asg.modules.security.entity.SystemRole;
import com.useeinfo.asg.modules.security.entity.SystemUser;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by tgq on 2017/5/8.
 */
@Controller
@RequestMapping("/mobile/login")
public class LoginManageAction {
    @Autowired
    private FrontUserBiz frontUserBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;

    /**
     * 用户登录页面
     */
    @RequestMapping("/user/gologin")
    public String  gologin(HttpServletRequest request, ModelMap mv) {
        return "mobile/login";
    }

    /**
     * 用户登录接口
     *
     * @param request
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping("/user/login")
    @ResponseBody
    public JSONObject login(HttpServletRequest request, String username, String password) {
        JSONObject jsonObject = CommUtil.jsonResult(false);
        if (!CommUtil.null2String(username).equals("") && !CommUtil.null2String(password).equals("")) {

            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("userName", username);
            queryParam.getSqlMap().put("password", password);
            List<FrontUser> frontUserBizList = frontUserBiz.findList(queryParam);
            if (frontUserBizList.size() > 0) {
                FrontUser user = frontUserBizList.get(0);
                jsonObject = CommUtil.jsonResult(true);
                request.getSession().setAttribute("user", user);
            } else {
                jsonObject.put("msg", "账号密码有误");
            }

        } else {
            jsonObject.put("msg", "账号密码不可为空");
        }
        return jsonObject;
    }

    @RequestMapping("/user/no_login")
    @ResponseBody
    public JSONObject no_login_role(HttpServletRequest request) {
        JSONObject jsonObject = CommUtil.jsonResult(false);
        jsonObject.put("msg", "用户未登录");
        return jsonObject;
    }

    @RequestMapping("/user/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        JSONObject jsonObject = CommUtil.jsonResult(true);
        return "mobile/login";
    }


    /**
     * 用户注册页面
     */
    @RequestMapping("/user/goregister")
    public String  goregister(HttpServletRequest request, ModelMap mv) {
        return "mobile/register";
    }


    /**
     * 用户注册
     * @param request
     * @return
     */
    @RequestMapping("/user/register")
    @ResponseBody
    public JSONObject register(HttpServletRequest request, String username, String password) {
        JSONObject jsonObject = CommUtil.jsonResult(false);
        if (!CommUtil.null2String(username).equals("") && !CommUtil.null2String(password).equals("")) {

            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("userName", username);
            List<FrontUser> frontUserBizList = frontUserBiz.findList(queryParam);
            if (frontUserBizList.size() > 0) {
                jsonObject.put("msg", "用户名已经存在");

            } else {
                FrontUser user = new FrontUser();
                user.setCreateTime(new Date());
                user.setPassword(CommUtil.null2String(password));
                user.setUserName(CommUtil.null2String(username));
                frontUserBiz.addOrUpdate(user);
                SystemUser systemUser = new SystemUser();
                systemUser.setCreateTime(new Date());
                systemUser.setUserAccount(CommUtil.null2String(username));
                systemUser.setUserName(CommUtil.null2String(username));
                systemUser.setUserPassword(CommUtil.null2String(password));
                systemUserBiz.addOrUpdate(systemUser);
                jsonObject = CommUtil.jsonResult(true);
                request.getSession().setAttribute("user", user);
            }
        } else {
            jsonObject.put("msg", "账号密码不可为空");
        }
        return jsonObject;
    }

}
